package com.demo.demospringboot.repository;

import com.demo.demospringboot.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Insert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 用户数据访问层
 */
@Mapper
public interface UserRepository {
  Logger logger = LoggerFactory.getLogger(UserRepository.class);

  /**
   * 根据用户名查询用户
   */
  @Select("SELECT * FROM users WHERE username = #{username}")
  default User findByUsername(@Param("username") String username) {
    logger.debug("执行SQL查询 - 根据用户名查询用户: {}", username);
    try {
      User user = findByUsernameInternal(username);
      if (user != null) {
        logger.debug("查询到用户 - 用户ID: {}, 用户名: {}", user.getId(), username);
      } else {
        logger.debug("未查询到用户 - 用户名: {}", username);
      }
      return user;
    } catch (Exception e) {
      logger.error("查询用户异常 - 用户名: {}, 异常: {}", username, e.getMessage(), e);
      throw e;
    }
  }

  /**
   * 内部查询方法
   */
  @Select("SELECT * FROM users WHERE username = #{username}")
  User findByUsernameInternal(@Param("username") String username);

  /**
   * 根据邮箱查询用户
   */
  @Select("SELECT * FROM users WHERE email = #{email}")
  default User findByEmail(@Param("email") String email) {
    logger.debug("执行SQL查询 - 根据邮箱查询用户: {}", email);
    try {
      User user = findByEmailInternal(email);
      if (user != null) {
        logger.debug("查询到用户 - 用户ID: {}, 邮箱: {}", user.getId(), email);
      } else {
        logger.debug("未查询到用户 - 邮箱: {}", email);
      }
      return user;
    } catch (Exception e) {
      logger.error("查询用户异常 - 邮箱: {}, 异常: {}", email, e.getMessage(), e);
      throw e;
    }
  }

  /**
   * 内部查询方法
   */
  @Select("SELECT * FROM users WHERE email = #{email}")
  User findByEmailInternal(@Param("email") String email);

  /**
   * 根据用户名和密码查询用户
   */
  @Select("SELECT * FROM users WHERE username = #{username} AND password = #{password}")
  default User findByUsernameAndPassword(@Param("username") String username, @Param("password") String password) {
    logger.debug("执行SQL查询 - 根据用户名和密码查询用户: {}", username);
    try {
      User user = findByUsernameAndPasswordInternal(username, password);
      if (user != null) {
        logger.debug("查询到用户 - 用户ID: {}, 用户名: {}", user.getId(), username);
      } else {
        logger.debug("未查询到用户 - 用户名: {}, 密码验证失败", username);
      }
      return user;
    } catch (Exception e) {
      logger.error("查询用户异常 - 用户名: {}, 异常: {}", username, e.getMessage(), e);
      throw e;
    }
  }

  /**
   * 内部查询方法
   */
  @Select("SELECT * FROM users WHERE username = #{username} AND password = #{password}")
  User findByUsernameAndPasswordInternal(@Param("username") String username, @Param("password") String password);

  /**
   * 创建新用户
   */
  @Insert("INSERT INTO users (username, password, email, name, age, create_time, update_time) " +
      "VALUES (#{username}, #{password}, #{email}, #{name}, #{age}, NOW(), NOW())")
  default int insertUser(User user) {
    logger.debug("执行SQL插入 - 创建新用户: {}", user.getUsername());
    try {
      int result = insertUserInternal(user);
      if (result > 0) {
        logger.info("用户创建成功 - 用户名: {}, 邮箱: {}", user.getUsername(), user.getEmail());
      } else {
        logger.error("用户创建失败 - 用户名: {}", user.getUsername());
      }
      return result;
    } catch (Exception e) {
      logger.error("创建用户异常 - 用户名: {}, 异常: {}", user.getUsername(), e.getMessage(), e);
      throw e;
    }
  }

  /**
   * 内部插入方法
   */
  @Insert("INSERT INTO users (username, password, email, name, age, create_time, update_time) " +
      "VALUES (#{username}, #{password}, #{email}, #{name}, #{age}, NOW(), NOW())")
  int insertUserInternal(User user);
}